/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visao.usuario;

import java.awt.*;
import javax.swing.*;
import modelo.persistencia.ControleAcesso;
import modelo.dominio.Usuario;
import modelo.persistencia.PersistenciaUsuario;

public class InterfaceAlterarUsuario extends javax.swing.JFrame{
	
	private final PersistenciaUsuario persistenciaUsuario = new PersistenciaUsuario();
	
	public InterfaceAlterarUsuario() {
		initComponents();
		
		this.setTitle("Alterações em Usuários");
		
		jButton1.setVisible(true);
		jButton2.setVisible(true);
		jButton3.setVisible(false);
		jButton4.setVisible(false);
		jButton5.setVisible(true);
		jLabel1.setVisible(true);
		jLabel2.setVisible(false);
		jLabel3.setVisible(false);
		jPanel1.setVisible(true);
		jTextField1.setEnabled(true);
		jTextField1.setVisible(true);
		jTextField2.setVisible(false);
		jTextField3.setVisible(false);
	}

	
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
     private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Alterando Dados", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu Light", 0, 18), new java.awt.Color(76, 76, 76))); // NOI18N

        jButton3.setText("Confirmar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmaActionPerformed(evt);
            }
        });

        jButton4.setText("Cancelar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel1.setText("Login Administrador : ");

        jLabel2.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel2.setText("Login Usuário : ");

        jLabel3.setFont(new java.awt.Font("Ubuntu Light", 0, 18)); // NOI18N
        jLabel3.setText("Senha Usuário : ");

        jButton5.setText("Ok");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(486, Short.MAX_VALUE)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(304, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(98, 98, 98)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(268, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addGap(103, 103, 103)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(jLabel3))
                    .addComponent(jLabel2)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(173, 173, 173)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap())
        );

        jButton1.setText("Logoff");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLogoffActionPerformed(evt);
            }
        });

        jButton2.setText("Voltar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 537, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(59, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 488, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }///editor-fold>

	private void jButtonLogoffActionPerformed(java.awt.event.ActionEvent evt) {                                         
		dispose();
		ControleAcesso.logoff();
	}                                        

	private void jButtonVoltarActionPerformed(java.awt.event.ActionEvent evt) {                                         
		dispose();
		ControleAcesso.voltarMenuPrincipal();
	}                                        

	private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {                                         
		jButton1.setVisible(true);
		jButton2.setVisible(true);
		jButton3.setVisible(false);
		jButton4.setVisible(false);
		jButton5.setVisible(true);
		jLabel1.setVisible(true);
		jLabel2.setVisible(false);
		jLabel3.setVisible(false);
		jPanel1.setVisible(true);
		jTextField1.setEnabled(true);
		jTextField1.setText("");
		jTextField1.setVisible(true);
		jTextField2.setVisible(false);
		jTextField3.setVisible(false);
	}                                        

	private void jButtonConfirmaActionPerformed(java.awt.event.ActionEvent evt) {                                         
		String login = jTextField2.getText();
		String senha = jTextField3.getText();
		String acesso = "";
		Usuario usuario = new Usuario(login, senha, acesso);
					   
		if(login.equals("") && senha.equals("")){
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Por favor, preencha pelo menos um campo!","Erro",JOptionPane.ERROR_MESSAGE);
		}else if(ControleAcesso.isAdmin(senha)){
			if(!login.equals("")){
				usuario.setNome(login);	
			}
			if(!senha.equals("")){
				usuario.setSenha(senha);	
			}
			jButton1.setVisible(true);
			jButton2.setVisible(true);
			jButton3.setVisible(false);
			jButton4.setVisible(false);
			jButton5.setVisible(true);
			jLabel1.setVisible(true);
			jLabel2.setVisible(false);
			jLabel3.setVisible(false);
			jPanel1.setVisible(true);
			jTextField1.setEnabled(true);
			jTextField1.setText("");
			jTextField1.setVisible(true);
			jTextField2.setText("");
			jTextField2.setVisible(false);
			jTextField3.setText("");
			jTextField3.setVisible(false);
			persistenciaUsuario.atualizar(usuario);
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Alteração concluída!","Parabéns!",JOptionPane.INFORMATION_MESSAGE);
		}
	}                                        

	private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {                                         
		String login = jTextField2.getText();
	
		if(!login.equals("")){
			if(!ControleAcesso.isAdmin(login)){
				jButton1.setVisible(true);
				jButton2.setVisible(true);
				jButton3.setVisible(true);
				jButton4.setVisible(true);
				jButton5.setVisible(true);
				jLabel1.setVisible(true);
				jLabel2.setVisible(true);
				jLabel3.setVisible(true);
				jPanel1.setVisible(true);
				jTextField1.setEnabled(false);
				jTextField1.setVisible(true);
				jTextField2.setVisible(true);
				jTextField3.setVisible(true);
			}else{
				JOptionPane.showMessageDialog(null,"<html> <font color='black'>Não exite Usuário com esse Login!","Erro",JOptionPane.ERROR_MESSAGE);
				jButton1.setVisible(true);
				jButton2.setVisible(true);
				jButton3.setVisible(false);
				jButton4.setVisible(false);
				jButton5.setVisible(true);
				jLabel1.setVisible(true);
				jLabel2.setVisible(false);
				jLabel3.setVisible(false);
				jPanel1.setVisible(true);
				jTextField1.setEnabled(true);
				jTextField1.setText("");
				jTextField1.setVisible(false);
				jTextField2.setText("");
				jTextField2.setVisible(false);
				jTextField3.setText("");
				jTextField3.setVisible(false);
			}
		}else{
			JOptionPane.showMessageDialog(null,"<html> <font color='black'>Digite um Login por favor!","Erro",JOptionPane.ERROR_MESSAGE);
		}
	}                                        

    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
}
